﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary
{
    public class Bits
    {
        private bool[] m_Bits;

        public Bits(int length)
        {
            m_Bits = new bool[length];
        }

        public Bits(bool[] bits)
            : this(bits.Length)
        {
            System.Array.Copy(bits, m_Bits,bits.Length);
        }

        public int Length
        {
            get
            {
                return m_Bits.Length;
            }
        }

        public bool this[int Index]
        {
            get
            {
                return m_Bits[Index];
            }
            set
            {
                m_Bits[Index] = value;
            }
        }

        public Bits GetBits(int startIndex, int endIndex)
        {
            int Length = endIndex - startIndex + 1;
            Bits Result = new Bits(Length);

            System.Array.Copy(m_Bits, startIndex, Result.m_Bits, 0, Length);

            return Result;
        }

        public override string ToString()
        {
            StringBuilder Result = new StringBuilder();

            foreach (bool b in m_Bits)
            {
                Result.Append(b ? "1" : "0");
            }

            return Result.ToString();
        }

        public override int GetHashCode()
        {
            return m_Bits.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }

            if ((obj == null) || !(obj is Bits))
            {
                return false;
            }

            Bits Another = obj as Bits;

            if (Length != Another.Length)
            {
                return false;
            }

            for (int i = 0; i < Length; i++)
            {
                if (m_Bits[i] != Another[i])
                {
                    return false;
                }
            }

            return true;
        }
    }
}
